﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.YouTube;
using Google.YouTube;

namespace YoutubeVideoManager
{
    /// <summary>
    /// Logica di interazione per SubscriptionManager.xaml
    /// </summary>
    public partial class SubscriptionManager : UserControl
    {
        #region Events
        public event SelectionChangedEventHandler SelectionChanged;
        #endregion

        #region Properties
        #endregion

        #region Fields
        BackgroundWorker worker;
        #endregion

        #region Methods
        #endregion

        public SubscriptionManager()
        {
            InitializeComponent();

            worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
            worker.WorkerSupportsCancellation = worker.WorkerReportsProgress = true;
        }

        void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            string title = e.UserState.ToString();
            //title = title.Substring(title.IndexOf(":") + 2);
            cmbSubscriptions.Items.Add(title);
        }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            Feed<Subscription> feed = Global.Request.GetSubscriptionsFeed("default");
            foreach (var entry in feed.Entries)
            {
                worker.ReportProgress(0, entry.UserName);
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            cmbSubscriptions.Items.Add("Playlists");
            cmbSubscriptions.SelectedIndex = 0;
            worker.RunWorkerAsync();
        }

        private void cmbSubscriptions_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (SelectionChanged != null)
                SelectionChanged(sender, e);
        }
    }
}
